package com.cloudeasy.user.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.api.vo.user.UserVO;
import com.cloudeasy.user.field.UserField;
import com.cloudeasy.user.po.User;
import com.cloudeasy.user.po.UserInfo;
import com.cloudeasy.user.service.UserInfoService;
import com.cloudeasy.user.service.UserService;
import com.cloudeasy.user.util.IPageUtil;
import com.cloudeasy.user.util.UserUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    private final UserInfoService userInfoService;

    public UserController(UserService userService, UserInfoService userInfoService) {
        this.userService = userService;
        this.userInfoService = userInfoService;
    }

    /**
     * 改变用户状态，只有超级管理员可以管理
     *
     * @param userId 目标用户Id
     * @return {@link Result <Boolean>}
     */
    @PutMapping("/lock/{userId}/{unlocked}")
    public Result<Boolean> changeLock(@PathVariable("userId") String userId, @PathVariable("unlocked") Boolean unlocked) {
        if (unlocked == null) {
            throw new CloudEasyException(ResultCodeEnum.PARAMETER_ERROR);
        }
        return ResultUtil.success(userService.changeLock(userId, unlocked));
    }

    /**
     * 获取用户账号关键信息
     *
     * @param ids      用户Id列表
     * @param accounts 账号列表
     * @param unlocked 是否开放
     * @param asc      正序排序字段
     * @param desc     倒序排序字段
     * @return {@link List<UserVO>}的{@link Result}
     */
    @GetMapping
    public Result<List<UserVO>> userList(@RequestParam(value = "ids", required = false) List<String> ids,
                                         @RequestParam(value = "accounts", required = false) List<String> accounts,
                                         @RequestParam(value = "unlocked", required = false) Boolean unlocked,
                                         @RequestParam(value = "asc", required = false) String[] asc,
                                         @RequestParam(value = "desc", required = false) String[] desc) {
        QueryWrapper<User> queryWrapper = UserUtil.compose(ids, accounts, unlocked, asc, desc);
        List<User> users = userService.list(queryWrapper);

        List<String> userIdList = users.stream().map(User::getId).collect(Collectors.toList());

        if (userIdList.size()==0){
            return ResultUtil.success();
        }

        List<UserInfo> userInfos = new ArrayList<>(userInfoService.listByIds(userIdList));
        return ResultUtil.success(UserUtil.convert(users, userInfos));
    }

    /**
     * 分页获取用户账号关键信息
     *
     * @param ids      用户Id列表
     * @param accounts 账号列表
     * @param unlocked 是否开放
     * @param asc      正序排序字段
     * @param desc     倒序排序字段
     * @param page     页数
     * @param size     每页大小
     * @return {@link PageResult<UserVO>}的{@link Result}
     */
    @GetMapping("/{page}/{size}")
    public Result<PageResult<UserVO>> userPage(@RequestParam(value = "ids", required = false) List<String> ids,
                                               @RequestParam(value = "accounts", required = false) List<String> accounts,
                                               @RequestParam(value = "unlocked", required = false) Boolean unlocked,
                                               @RequestParam(value = "asc", required = false) String[] asc,
                                               @RequestParam(value = "desc", required = false) String[] desc,
                                               @PathVariable("page") Integer page,
                                               @PathVariable("size") Integer size) {
        QueryWrapper<User> queryWrapper = UserUtil.compose(ids, accounts, unlocked, asc, desc);
        IPage<User> userTempIPage = IPageUtil.compose(page, size);
        IPage<User> userIPage = userService.page(userTempIPage, queryWrapper);

        List<String> userIdList = userIPage.getRecords().stream().map(User::getId).collect(Collectors.toList());

        if (userIdList.size()==0){
            return ResultUtil.success();
        }

        List<UserInfo> userInfos = new ArrayList<>(userInfoService.listByIds(userIdList));

        return ResultUtil.success(UserUtil.convert(userIPage, userInfos));
    }
}
